package payment.mobilepos.unionpay.com.demo;

import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.com.unionpay.speech.util.Money2Digit;
import com.sdk.ActiveMode;
import com.sdk.IActivity;
import com.sdk.LogUtils;
import com.sdk.SpeechSynthesizerUtil;

import java.util.ArrayList;
import java.util.List;

import payment.mobilepos.unionpay.com.R;
import payment.mobilepos.unionpay.com.demo.base.UPMPosBaseActivity;

public class PayeeSwipeActivity extends UPMPosBaseActivity implements IActivity {

    private TextView mAmountText;

    private String mAmount = "0";

    protected Handler mainHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //initialTts();
        startSpeakText();


        setContentView(R.layout.activity_payee_swipe);


        ImageView backImgView = findViewById(R.id.nav_btn_left);
        backImgView.setImageResource(R.mipmap.ic_back);
        backImgView.setOnClickListener(this);
        findViewById(R.id.nav_btn_right).setVisibility(View.GONE);

        mAmountText = findViewById(R.id.text_amount);
        mAmount = getIntent().getStringExtra("amount");
        showAmountText(mAmount);
    }

    private void showAmountText(String amount) {

        if (amount.contains(".")) {
            int pointIndex = amount.indexOf(".");
            int length = amount.length();
            for (int i = 2; i > length - 1 - pointIndex; i--) {
                amount += "0";
            }
        } else {
            amount += ".00";
        }

        SpannableString span = new SpannableString("￥" + amount);
        span.setSpan(new AbsoluteSizeSpan(34, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int pointIndex = amount.indexOf(".");
        span.setSpan(new AbsoluteSizeSpan(34, true), pointIndex + 1, amount.length() + 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mAmountText.setText(span);

        // 语音合成
        speakText("请付款" + amount + "元，请刷卡或者扫码");
    }

    @Override
    public void onClick(View v) {

        // 终止合成
        mBaiDuSpeechUtil.stopSpeechSynthesizer();
        super.onClick(v);
        switch (v.getId()) {
            case R.id.nav_btn_left:
                finish();
                break;
        }
    }


    @Override
    protected void onPause() {
        overridePendingTransition(0, 0);
        super.onPause();
        mBaiDuSpeechUtil.endSpeechSynthesizer();
    }

    @Override
    public List<Enum<ActiveMode>> getSpeechActiveMode() {
        List<Enum<ActiveMode>> modes = new ArrayList<>();
        modes.add(ActiveMode.BLUETOOTH);
        modes.add(ActiveMode.CLICK);
        modes.add(ActiveMode.SPEECH_WAKEUP);
        modes.add(ActiveMode.MQTT);

        return modes;
    }

    @Override
    public void doSpeechEnd(String result) {
        if (!TextUtils.isEmpty(result)) {
            if (result.contains("二维码") || result.contains("扫码")) {
                ActivityUtils.showBaiduActivity(this);
                finish();
                return;
            }
            String money = Money2Digit.convertToPinyin2Num(result);

            if ("".equals(money) || null == money
                    || "0".equals(money)) {
                // Log.i(TAG, "handleMessage: " + msg);
            } else {
                showAmountText(money);
            }
        } else {
            LogUtils.e("内容错误");
        }
    }

    @Override
    public boolean showEasyTouch() {
        return true;
    }


    private SpeechSynthesizerUtil mBaiDuSpeechUtil;

    //   语音初始化及播报
    private void startSpeakText() {
        mBaiDuSpeechUtil = SpeechSynthesizerUtil.getInstance();
        mBaiDuSpeechUtil.setInitialTts(this);

    }

    private void speakText(String content) {
        if (mBaiDuSpeechUtil != null) {
            mBaiDuSpeechUtil.speakText(content);
        }
    }

}
